package com.hccl.service.parser;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import static com.hccl.config.Constants.homonymPath;

/**
 * 预处理类。对识别后的结果进行纠错，比如荷包替换为和包。
 */
public class PreprocessUtil {

    private List<String[]> dics;

    public PreprocessUtil() {
        dics = new ArrayList<>();
        try (BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(new FileInputStream(homonymPath), "utf-8"))) {
            String word = "";
            while ((word = bufferedReader.readLine()) != null) {
                String tar = word.trim().split("\t")[0];
                String replacement = word.trim().split("\t")[1];
                String[] dic = {tar, replacement};
                dics.add(dic);
            }
        }catch(Exception e) {

        }
    }

    /**
     * description: 对识别后的结果进行纠错
     * @Param: 用户输入识别文本
     * @return 同音词纠错后的识别文本
     */
    public String preprocess(String line) throws Exception {
        String inputPrepro = "";
        if(dics != null) {
            for(String[] i : dics) {
                inputPrepro = line.replace(i[0], i[1]);
            }
        }
        return inputPrepro;
    }
}
